package it.trekker.dialogs;

import it.trekker.R;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class NewExcursionDialog extends Dialog {
	
	// GUI Components
	private EditText excursionName_et = null;
	private Button okBt = null;
	private Button cancelBt = null;
	
	private INewExcursion parent = null;
	
	public NewExcursionDialog(INewExcursion parent) {
		
		super(parent.getContext());
		this.parent = parent;

		setContentView(R.layout.new_excursion_dialog);
		setTitle("New Excursion");

		excursionName_et = (EditText) findViewById(R.id.excursion_name);
		excursionName_et.requestFocus();
//		excursionName_et.setOnFocusChangeListener(new EditTextFocusChangeListener());
		
		Button okBt = (Button) findViewById(R.id.ok_bt);
		okBt.setOnClickListener(new OkButtonListener());
		
		Button cancelBt = (Button) findViewById(R.id.cancel_bt);
		cancelBt.setOnClickListener(new CancelButtonListener());
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

	}
	
	public String getExcursionName() {
		return excursionName_et.getText().toString();
	}
	
	public void setOkButtonListener(View.OnClickListener listener) {
		okBt.setOnClickListener(listener);
	}
	
	public void setCancelButtonListener(View.OnClickListener listener) {
		cancelBt.setOnClickListener(listener);
	}
	
	
	
	//************************************************************//
  	//********************* Listener classes *********************//
  	//************************************************************//
	
	private class OkButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// Hide soft keyboard
			InputMethodManager imm = (InputMethodManager)parent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(excursionName_et.getWindowToken(), 0);
			
			parent.createNewExcursion(getExcursionName());
			dismiss();
		}
		
	} 
	
	private class CancelButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// Hide soft keyboard
			InputMethodManager imm = (InputMethodManager)parent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(excursionName_et.getWindowToken(), 0);
						
			dismiss();
		}
		
	} 
	
	private class EditTextFocusChangeListener implements View.OnFocusChangeListener {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus) {
	            NewExcursionDialog.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
	        }
		}
		
	}
	
	
	
	
	
	
	public interface INewExcursion {
		public Context getContext();
		public void createNewExcursion(String name);
	}
}
